﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="AzureManagementApiTests.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagementTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;

    using AzureManagement;
    using AzureManagement.AzureDataContracts;

    using AzureManagementTests.TestSupport;

    using Common;

    using Xunit;

    public class AzureManagementApiTests
    {
        private const string Prefix = "test";

        private const string BaseUri = Prefix + "://management.core.windows.net/";

        private const string ManagementCertificateThumbprint = "f5af408cdece42fd6b5f35c8889c0e22ac1c5ec2";

        private const string ApiVersion = "2012-08-01";

        private const string SubscriptionId = "b4249f2a-2378-4c3e-8e5a-113145d7f1c0";

        private static readonly AzureManagementTestWebRequestCreate TestWebRequestCreate;

        static AzureManagementApiTests()
        {
            TestWebRequestCreate = new AzureManagementTestWebRequestCreate
            {
                BaseUri = BaseUri,
                ApiVersion = ApiVersion,
                SubscriptionId = SubscriptionId,
                ManagementCertificates = new List<string>
                {
                    ManagementCertificateThumbprint
                },
                Operations = CreateOperations(),
                HostedServices = CreateHostedServices(),
                StorageAccounts = CreateStorageAccounts()
            };

            // will use the method at http://blog.salamandersoft.co.uk/index.php/2009/10/how-to-mock-httpwebrequest-when-unit-testing/
            WebRequest.RegisterPrefix(Prefix, TestWebRequestCreate);
        }

        [Fact]
        public void CanListHostedServices()
        {
            var serviceContext = new AzureServiceContext(
                                                new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                                new Uri(BaseUri),
                                                ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var list = azureService.ListHostedServices().Result;

            Assert.NotNull(list);
            Assert.True(list.Count == TestWebRequestCreate.HostedServices.Count);
        }

        [Fact]
        public void CanGetHostedServiceProperties()
        {
            var serviceContext = new AzureServiceContext(
                                                new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                                new Uri(BaseUri),
                                                ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedServiceMock = TestWebRequestCreate.HostedServices.Select(x => x.HostedService)
                                                                       .First(x => x.ServiceName.Equals("testhostedservice2"));

            var hostedServiceProperties = azureService.GetHostedServiceProperties(hostedServiceMock.ServiceName).Result;

            Assert.NotNull(hostedServiceProperties);
            Assert.True(hostedServiceProperties.Url == hostedServiceMock.Url);
            Assert.True(hostedServiceProperties.ServiceName == hostedServiceMock.ServiceName);

            Assert.NotNull(hostedServiceProperties.HostedServiceProperties);
            Assert.NotNull(hostedServiceProperties.HostedServiceProperties.Description == hostedServiceMock.HostedServiceProperties.Description);
            Assert.NotNull(hostedServiceProperties.HostedServiceProperties.AffinityGroup == hostedServiceMock.HostedServiceProperties.AffinityGroup);
            Assert.NotNull(hostedServiceProperties.HostedServiceProperties.Location == hostedServiceMock.HostedServiceProperties.Location);
            Assert.NotNull(hostedServiceProperties.HostedServiceProperties.Label == hostedServiceMock.HostedServiceProperties.Label);

            Assert.Empty(hostedServiceProperties.Deployments);
        }

        [Fact]
        public void CanGetHostedServicePropertiesWithDetailsEmbedded()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedServiceMock = TestWebRequestCreate.HostedServices.Select(x => x.HostedService)
                                                                       .First(x => x.ServiceName.Equals("testhostedservice2"));

            var hostedServiceProperties = azureService.GetHostedServiceProperties(hostedServiceMock.ServiceName, true).Result;

            Assert.NotNull(hostedServiceProperties);
            Assert.True(hostedServiceProperties.Url == hostedServiceMock.Url);
            Assert.True(hostedServiceProperties.ServiceName == hostedServiceMock.ServiceName);

            Assert.NotNull(hostedServiceProperties.HostedServiceProperties);
            Assert.NotNull(hostedServiceProperties.HostedServiceProperties.Description == hostedServiceMock.HostedServiceProperties.Description);
            Assert.NotNull(hostedServiceProperties.HostedServiceProperties.AffinityGroup == hostedServiceMock.HostedServiceProperties.AffinityGroup);
            Assert.NotNull(hostedServiceProperties.HostedServiceProperties.Location == hostedServiceMock.HostedServiceProperties.Location);
            Assert.NotNull(hostedServiceProperties.HostedServiceProperties.Label == hostedServiceMock.HostedServiceProperties.Label);

            Assert.NotEmpty(hostedServiceProperties.Deployments);
        }

        [Fact]
        public void CantGetHostedServicePropertiesWhenServiceNameIsNullOrEmpty()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            Assert.Throws<AggregateException>(() => azureService.GetHostedServiceProperties(null).Result);
        }

        [Fact]
        public void CanCreateHostedService()
        {
            var serviceContext = new AzureServiceContext(
                                                new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                                new Uri(BaseUri),
                                                ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            azureService.CreateHostedService(
                                        "testhostedservice", 
                                        "test hosted service label", 
                                        "New hosted service", 
                                        "West US", 
                                        null).Wait();

            var createdHostedService = azureService.GetHostedServiceProperties("testhostedservice").Result;

            Assert.NotNull(createdHostedService);
            Assert.True(createdHostedService.ServiceName == "testhostedservice");

            Assert.NotNull(createdHostedService.HostedServiceProperties);
            Assert.NotNull(createdHostedService.HostedServiceProperties.Description == "New hosted service");
            Assert.Null(createdHostedService.HostedServiceProperties.AffinityGroup);
            Assert.NotNull(createdHostedService.HostedServiceProperties.Location == "West US");
            Assert.NotNull(createdHostedService.HostedServiceProperties.Label == TextHelper.EncodeToBase64("test hosted service label"));

            Assert.Empty(createdHostedService.Deployments);
        }

        [Fact]
        public void CanDeleteHostedService()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedServiceName = Guid.NewGuid().ToString();
            azureService.CreateHostedService(
                                    hostedServiceName, 
                                    "test hosted service label", 
                                    "New hosted service", 
                                    "West US", 
                                    null).Wait();

            var createdHostedService = azureService.GetHostedServiceProperties(hostedServiceName).Result;

            Assert.NotNull(createdHostedService);
            Assert.True(createdHostedService.ServiceName == hostedServiceName);

            azureService.DeleteHostedService(hostedServiceName).Wait();

            var hostedServices = azureService.ListHostedServices().Result;
            Assert.False(hostedServices.Any(x => x.ServiceName.Equals(hostedServiceName)));
        }

        [Fact]
        public void CanGetDeploymentByName()
        {
            var serviceContext = new AzureServiceContext(
                                                new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                                new Uri(BaseUri),
                                                ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedService = TestWebRequestCreate.HostedServices.Select(x => x.HostedService).First();
            var hostedServiceDeployment = hostedService.Deployments.First();

            var deployment = azureService.GetDeployment(hostedService.ServiceName, hostedServiceDeployment.Name).Result;

            Assert.NotNull(deployment);
            Assert.Equal(deployment.Name, hostedServiceDeployment.Name);
            Assert.Equal(deployment.Label, hostedServiceDeployment.Label);
            Assert.Equal(deployment.PrivateID, hostedServiceDeployment.PrivateID);
            Assert.Equal(deployment.Configuration, hostedServiceDeployment.Configuration);
            Assert.Equal(deployment.CreatedTime, hostedServiceDeployment.CreatedTime);
            Assert.Equal(deployment.LastModifiedTime, hostedServiceDeployment.LastModifiedTime);
            Assert.Equal(deployment.Status, hostedServiceDeployment.Status);
            Assert.Equal(deployment.SdkVersion, hostedServiceDeployment.SdkVersion);
            Assert.Equal(deployment.Locked, hostedServiceDeployment.Locked);
            Assert.Equal(deployment.RollbackAllowed, hostedServiceDeployment.RollbackAllowed);
            Assert.Equal(deployment.Url, hostedServiceDeployment.Url);
        }

        [Fact]
        public void CanGetDeploymentBySlot()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedService = TestWebRequestCreate.HostedServices.Select(x => x.HostedService).First();
            var hostedServiceDeployment = hostedService.Deployments.First();

            var deployment = azureService.GetDeployment(hostedService.ServiceName, hostedServiceDeployment.DeploymentSlot).Result;

            Assert.NotNull(deployment);
            Assert.Equal(deployment.Name, hostedServiceDeployment.Name);
            Assert.Equal(deployment.Label, hostedServiceDeployment.Label);
            Assert.Equal(deployment.PrivateID, hostedServiceDeployment.PrivateID);
            Assert.Equal(deployment.Configuration, hostedServiceDeployment.Configuration);
            Assert.Equal(deployment.CreatedTime, hostedServiceDeployment.CreatedTime);
            Assert.Equal(deployment.LastModifiedTime, hostedServiceDeployment.LastModifiedTime);
            Assert.Equal(deployment.Status, hostedServiceDeployment.Status);
            Assert.Equal(deployment.SdkVersion, hostedServiceDeployment.SdkVersion);
            Assert.Equal(deployment.Locked, hostedServiceDeployment.Locked);
            Assert.Equal(deployment.RollbackAllowed, hostedServiceDeployment.RollbackAllowed);
            Assert.Equal(deployment.Url, hostedServiceDeployment.Url);
        }

        [Fact]
        public void CanCreateDeployment()
        {
            var serviceContext = new AzureServiceContext(
                                                new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                                new Uri(BaseUri),
                                                ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedService = TestWebRequestCreate.HostedServices.Select(x => x.HostedService).First();

            var deploymentName = Guid.NewGuid().ToString();
            const DeploymentSlots DeploymentSlot = DeploymentSlots.Staging;
            azureService.CreateDeployment(
                            hostedService.ServiceName,
                            DeploymentSlot,
                            new DeploymentDetails
                            {
                                Name = deploymentName,
                                Configuration = "configuration",
                                Label = string.Format("{0} deployment", deploymentName),
                                PackageUrl = "packageurl",
                                StartDeployment = true,
                                TreatWarningsAsError = false
                            }).Wait();

            var createdDeployment = azureService.GetDeployment(hostedService.ServiceName, DeploymentSlot).Result;
            Assert.NotNull(createdDeployment);
        }

        [Fact]
        public void CanDeleteDeploymentByName()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedService = TestWebRequestCreate.HostedServices.Select(x => x.HostedService).First();

            var deploymentName = Guid.NewGuid().ToString();
            const DeploymentSlots DeploymentSlot = DeploymentSlots.Staging;
            azureService.CreateDeployment(
                hostedService.ServiceName,
                DeploymentSlot,
                new DeploymentDetails
                {
                    Name = deploymentName,
                    Configuration = "configuration",
                    Label = string.Format("{0} deployment", deploymentName),
                    PackageUrl = "packageurl",
                    StartDeployment = true,
                    TreatWarningsAsError = false
                }).Wait();

            var createdDeployment = azureService.GetDeployment(hostedService.ServiceName, DeploymentSlot).Result;
            Assert.NotNull(createdDeployment);

            azureService.DeleteDeployment(hostedService.ServiceName, createdDeployment.Name).Wait();

            Assert.False(hostedService.Deployments.Any(x => x.Name.Equals(deploymentName)));
        }

        [Fact]
        public void CanDeleteDeploymentBySlot()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedService = TestWebRequestCreate.HostedServices.Select(x => x.HostedService).First();

            var deploymentName = Guid.NewGuid().ToString();
            const DeploymentSlots DeploymentSlot = DeploymentSlots.Staging;
            azureService.CreateDeployment(
                                hostedService.ServiceName,
                                DeploymentSlot,
                                new DeploymentDetails
                                {
                                    Name = deploymentName,
                                    Configuration = "configuration",
                                    Label = string.Format("{0} deployment", deploymentName),
                                    PackageUrl = "packageurl",
                                    StartDeployment = true,
                                    TreatWarningsAsError = false
                                }).Wait();

            var createdDeployment = azureService.GetDeployment(hostedService.ServiceName, DeploymentSlot).Result;
            Assert.NotNull(createdDeployment);

            azureService.DeleteDeployment(hostedService.ServiceName, createdDeployment.DeploymentSlot).Wait();

            Assert.False(hostedService.Deployments.Any(x => x.DeploymentSlot.Equals(DeploymentSlot)));
        }

        [Fact]
        public void CanListStorageAccounts()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var storageAccounts = azureService.ListStorageAccounts().Result;
            Assert.NotEmpty(storageAccounts);
            Assert.True(storageAccounts.Count.Equals(TestWebRequestCreate.StorageAccounts.Count));
        }

        [Fact]
        public void CanCreateStorageAccount()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            const string TestStorageAccountName = "teststorageaccount";
            azureService.CreateStorageAccount(
                                TestStorageAccountName, 
                                "test storage account", 
                                "test storage account", 
                                string.Empty, 
                                "North US", 
                                true).Wait();

            var storageAccounts = azureService.ListStorageAccounts().Result;
            var createdStorageAccount = storageAccounts.FirstOrDefault(x => x.ServiceName.Equals(TestStorageAccountName));
            Assert.NotNull(createdStorageAccount);
        }

        [Fact]
        public void CanGetStorageAccountKeys()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var storageAccount = TestWebRequestCreate.StorageAccounts.First();
            var storageServiceKeys = azureService.GetStorageAccountKeys(storageAccount.ServiceName).Result;

            Assert.NotNull(storageServiceKeys);
            Assert.Equal(storageAccount.StorageServiceKeys.Primary, storageServiceKeys.Primary);
            Assert.Equal(storageAccount.StorageServiceKeys.Secondary, storageServiceKeys.Secondary);
        }

        [Fact]
        public void CanDeleteStorageAccount()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var storageAccount = TestWebRequestCreate.StorageAccounts.First();
            azureService.DeleteStorageAccount(storageAccount.ServiceName).Wait();

            var storageAccounts = azureService.ListStorageAccounts().Result;
            Assert.False(storageAccounts.Any(x => x.ServiceName.Equals(storageAccount.ServiceName)));
        }

        [Fact]
        public void CanListServiceCertificates()
        {
            var serviceContext = new AzureServiceContext(
                                        new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                        new Uri(BaseUri),
                                        ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedServiceMock = TestWebRequestCreate.HostedServices.First();
            var certificates = azureService.ListServiceCertificates(hostedServiceMock.HostedService.ServiceName).Result;

            Assert.Equal(certificates.Count, hostedServiceMock.Certificates.Count);
        }

        [Fact]
        public void CanDeleteCertificate()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var hostedServiceMock = TestWebRequestCreate.HostedServices.First();
            var certificate = hostedServiceMock.Certificates.First();

            azureService.DeleteServiceCertificate(
                                        hostedServiceMock.HostedService.ServiceName, 
                                        certificate.ThumbprintAlgorithm, 
                                        certificate.Thumbprint).Wait();

            var certificates = azureService.ListServiceCertificates(hostedServiceMock.HostedService.ServiceName).Result;
            Assert.False(certificates.Any(x => x.Thumbprint.Equals(certificate.Thumbprint)));
        }

        [Fact]
        public void CanGetOperationStatus()
        {
            var serviceContext = new AzureServiceContext(
                                                new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                                new Uri(BaseUri),
                                                ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            var operationMock = TestWebRequestCreate.Operations.First();
            var operation = azureService.GetOperationStatus(operationMock.ID).Result;

            Assert.NotNull(operation);
            Assert.Equal(operation.ID, operationMock.ID);
            Assert.Equal(operation.Status, operationMock.Status);
            Assert.Equal(operation.HttpStatusCode, operationMock.HttpStatusCode);
        }

        [Fact]
        public void CantGetOperationStatusWhenIdIsNullOrEmpty()
        {
            var serviceContext = new AzureServiceContext(
                                            new TestServiceCredentials(SubscriptionId, ManagementCertificateThumbprint),
                                            new Uri(BaseUri),
                                            ApiVersion);

            var azureService = new AzureServiceManagementClient(serviceContext);

            Assert.Throws<AggregateException>(() => azureService.GetOperationStatus(null).Result);
        }

        private static IList<Operation> CreateOperations()
        {
            return new List<Operation>
            {
                new Operation
                {
                    ID = "6C7B662E-4136-41DC-B08D-C794BD207043", 
                    HttpStatusCode = 200, 
                    Status = OperationStatuses.Succeeded
                }, 
                new Operation
                {
                    ID = "8889878C-696F-4052-BA81-C364DDDB6075", 
                    HttpStatusCode = 401, 
                    Status = OperationStatuses.Failed, 
                    Error = new OperationError
                    {
                        Code = "Invalid request", 
                        Message = "Invalid request"
                    }
                }, 
                new Operation
                {
                    ID = "C4EAEB94-B0D9-4333-B4B6-60564CEB9E79", 
                    HttpStatusCode = 200, 
                    Status = OperationStatuses.InProgress
                }
            };
        }

        private static IList<HostedServiceTestMock> CreateHostedServices()
        {
            return new List<HostedServiceTestMock>
            {
                new HostedServiceTestMock
                {
                    HostedService = new HostedService
                    {
                        ServiceName = "testhostedservice1",
                        Url = string.Format("{0}://testhostedservice1.cloudapp.net", Prefix),
                        HostedServiceProperties = 
                        new HostedServiceProperties
                        {
                            Description = "New cloud service",
                            Location = "West US",
                            Label = "YTp9cmVyY29wZQ==",
                            Status = HostedServiceStatuses.Created,
                            DateCreated = DateTime.Parse("2013-02-05T10:31:51Z"),
                            DateLastModified = DateTime.Parse("2013-02-14T09:47:16Z"),
                        },
                        Deployments = new List<Deployment>
                        {
                            new Deployment
                            {
                                Name = "c57fb2e71d9c47bb9c8f05ef204d3bc7",
                                DeploymentSlot = DeploymentSlots.Production,
                                PrivateID = "1e0d745deffb4a7b82383b4655123746",
                                Status = DeploymentStatuses.Running,
                                Label = "dGVzdGhvc3RlZHNlcnZpY2Ux",
                                Url = string.Format("{0}://testhostedservice1.cloudapp.net", Prefix),
                                UpgradeDomainCount = 1,
                                SdkVersion = "1.8.31004.1351",
                                Locked = false,
                                RollbackAllowed = false,
                                CreatedTime = DateTime.Parse("2013-02-05T10:31:51Z"),
                                LastModifiedTime = DateTime.Parse("2013-02-14T09:47:16Z"),
                                PersistentVMDowntime = new PersistentVMDowntime
                                {
                                    StartTime = DateTime.Parse("2013-01-22T00:00:00Z"),
                                    EndTime = DateTime.Parse("2013-02-25T06:00:00Z"),
                                    Status = "PersistentVMUpdateCompleted"
                                }
                            }
                        }
                    },
                    Certificates = new List<Certificate>
                    {
                        new Certificate
                        {
                            CertificateUrl = "certificateurl1",
                            Data = TextHelper.EncodeToBase64("certificate1 data"),
                            Thumbprint = "31AC7B97D64C45228AD1263A6568A099",
                            ThumbprintAlgorithm = "sha1"
                        }
                    }
                },
                new HostedServiceTestMock
                {
                    HostedService = new HostedService
                    {
                        ServiceName = "testhostedservice2",
                        Url = string.Format("{0}://testhostedservice2.cloudapp.net", Prefix),
                        HostedServiceProperties = new HostedServiceProperties
                        {
                            Description = "New cloud service",
                            Location = "North US",
                            Label = "YZp3ceVyE29wZQ==",
                            Status = HostedServiceStatuses.Created,
                            DateCreated = DateTime.Parse("2013-01-03T10:31:51Z"),
                            DateLastModified = DateTime.Parse("2013-02-14T09:47:16Z"),
                        },
                        Deployments = new List<Deployment>
                        {
                            new Deployment
                            {
                                Name = "c67fb2e51d9d47bb9c8g31eg412f3bc7",
                                DeploymentSlot = DeploymentSlots.Production,
                                PrivateID = "2f4e745deffb4a7b82383b4655123746",
                                Status = DeploymentStatuses.Suspended,
                                Label = "dGVzdGhvc3RlZHNlcnZpY2Uy",
                                Url = string.Format("{0}://testhostedservice2.cloudapp.net", Prefix),
                                UpgradeDomainCount = 1,
                                SdkVersion = "1.8.31004.1351",
                                Locked = false,
                                RollbackAllowed = false,
                                CreatedTime = DateTime.Parse("2013-02-05T10:31:51Z"),
                                LastModifiedTime = DateTime.Parse("2013-02-14T09:47:16Z"),
                                PersistentVMDowntime = new PersistentVMDowntime
                                {
                                    StartTime = DateTime.Parse("2013-01-22T00:00:00Z"),
                                    EndTime = DateTime.Parse("2013-02-25T06:00:00Z"),
                                    Status = "PersistentVMUpdateCompleted"
                                }
                            }
                        }
                    },
                    Certificates = new List<Certificate>
                    {
                        new Certificate
                        {
                            CertificateUrl = "certificateurl2",
                            Data = TextHelper.EncodeToBase64("certificate2 data"),
                            Thumbprint = "4368FE971C8643979561F9FB4D2A5B04",
                            ThumbprintAlgorithm = "sha1"
                        }
                    }
                },
                new HostedServiceTestMock
                {
                    HostedService = new HostedService
                    {
                        ServiceName = "testhostedservice3",
                        Url = string.Format("{0}://testhostedservice3.cloudapp.net", Prefix),
                        HostedServiceProperties = new HostedServiceProperties
                        {
                            Description = "New cloud service",
                            Location = "South US",
                            Label = "FYd7cmVyZ29wZQ==",
                            Status = HostedServiceStatuses.Created,
                            DateCreated = DateTime.Parse("2013-01-02T10:31:51Z"),
                            DateLastModified = DateTime.Parse("2013-02-14T09:47:16Z"),
                        },
                        Deployments = new List<Deployment>
                        {
                            new Deployment
                            {
                                Name = "c67fb2e51d9d47bb9e4g12sf541g3bd7",
                                DeploymentSlot = DeploymentSlots.Staging,
                                PrivateID = "2g4e745deggc5d7b82533b4655123746",
                                Status = DeploymentStatuses.RunningTransitioning,
                                Label = "dGVzdGhvc3RlZHNlcnZpY2Uz",
                                Url = string.Format("{0}://testhostedservice3.cloudapp.net", Prefix),
                                UpgradeDomainCount = 1,
                                SdkVersion = "1.8.31004.1351",
                                Locked = false,
                                RollbackAllowed = false,
                                CreatedTime = DateTime.Parse("2013-02-05T10:31:51Z"),
                                LastModifiedTime = DateTime.Parse("2013-02-14T09:47:16Z"),
                                PersistentVMDowntime = new PersistentVMDowntime
                                {
                                    StartTime = DateTime.Parse("2013-01-22T00:00:00Z"),
                                    EndTime = DateTime.Parse("2013-02-25T06:00:00Z"),
                                    Status = "PersistentVMUpdateCompleted"
                                }
                            }
                        }
                    },
                    Certificates = new List<Certificate>
                    {
                        new Certificate
                        {
                            CertificateUrl = "certificateurl3",
                            Data = TextHelper.EncodeToBase64("certificate3 data"),
                            Thumbprint = "D23F6F6C008B47D9885EF1E4DFF69FF3",
                            ThumbprintAlgorithm = "sha1"
                        }
                    }
                }
            };
        }

        private static IList<StorageService> CreateStorageAccounts()
        {
            return new List<StorageService>
            {
                new StorageService
                {
                    ServiceName = "teststorageaccount1", 
                    Url = string.Format("{0}/{1}/services/storageservices/teststorageaccount1", BaseUri, SubscriptionId), 
                    StorageServiceProperties = new StorageServiceProperties
                    {
                        Description = "test storage account 1", 
                        Location = "West US", 
                        AffinityGroup = string.Empty, 
                        GeoReplicationEnabled = false, 
                        Label = TextHelper.EncodeToBase64("test storage account 1"), 
                        Status = StorageServiceStatuses.Created
                    },
                    StorageServiceKeys = new StorageServiceKeys
                    {
                        Primary = "7CAA8CA0-70FB-4B7C-AD3C-9A22D0376AEB", 
                        Secondary = "66A76B34-3E9E-4808-9F9F-28AFA426B47A"
                    }
                },
                new StorageService
                {
                    ServiceName = "teststorageaccount2", 
                    Url = string.Format("{0}/{1}/services/storageservices/teststorageaccount2", BaseUri, SubscriptionId), 
                    StorageServiceProperties = new StorageServiceProperties
                    {
                        Description = "test storage account 2", 
                        Location = "North US", 
                        AffinityGroup = string.Empty, 
                        GeoReplicationEnabled = false, 
                        Label = TextHelper.EncodeToBase64("test storage account 2"), 
                        Status = StorageServiceStatuses.Created
                    }, 
                    StorageServiceKeys = new StorageServiceKeys
                    {
                        Primary = "74A0F9A2-7E49-4295-B457-83CAD6CFC608", 
                        Secondary = "BA29FB4C-E0B4-453F-A91D-4A31F51749A4"
                    }
                }
            };
        }
    }
}